package st.business.tepth.business.user.busnumber.finishholder.repair;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.CompleteInfoBean;
import st.business.tepth.business.businfo.BusInfoDelegate;
import st.business.tepth.business.event.ReloadBusNumberDataEvent;
import st.business.tepth.business.event.StartBrotherEvent;
import st.business.tepth.business.user.busnumber.workholder.maintain.finish.DialogFinishNotPass;
import st.business.tepth.business.user.busnumber.workholder.repair.finish.RepairCompleteDataConvert;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-11 15:37
 * 小修已完成
 */

public class FinishRepairDelegate extends MvpFragment<FinishRepairPresenter> implements FinishRepairContract.View {
    public static FinishRepairDelegate newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        FinishRepairDelegate delegate = new FinishRepairDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_work_type)
    TextView mTvType;
    @BindView(R2.id.tv_cancel_work)
    TextView mTvReWork;
    private FinishRepairAdapter mFinishWorkAdapter;
    private int mRepairId;
    private DialogLoader mDialogLoader;
    private CompleteInfoBean mCompleteInfoBean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("id");
        }
    }

    @Override
    public FinishRepairPresenter getPresenter() {
        return new FinishRepairPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_repair_finish_work;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerView.setAdapter(mFinishWorkAdapter = new FinishRepairAdapter(R.layout.item_complete_check_un_edit,
                null));
        mDialogLoader = new DialogLoader(_mActivity);

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.getInfo(UserManager.getInstance().getToken(), mRepairId
        );
    }

    @Override
    public void showInfos(CompleteInfoBean infoBean) {
        mCompleteInfoBean = infoBean;
        mTvType.setText(infoBean.getTypeName());
        if (mFinishWorkAdapter != null) {
            mFinishWorkAdapter.setNewData(infoBean.getCompleteList());
        }
        if (infoBean.isWorker() && (infoBean.getStatus() == 8 ||
                infoBean.getStatus() == 9)) {
            mTvReWork.setVisibility(View.VISIBLE);
        } else {
            mTvReWork.setVisibility(View.GONE);
        }
    }

    @OnClick(R2.id.tv_bus_info)
    public void onShowBusInfo() {
        EventBus.getDefault().post(new StartBrotherEvent(BusInfoDelegate.newInstance(mRepairId)));
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @Override
    public void reworkSucceed() {
        mDialogLoader.stopProgressWithSuccess("返工成功", "请确认", new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                EventBus.getDefault().post(new ReloadBusNumberDataEvent());
            }
        });
    }

    @OnClick(R2.id.tv_cancel_work)
    public void onRework() {
        new DialogFinishNotPass(_mActivity,"竣检返工", RepairCompleteDataConvert.convertToClassification(mCompleteInfoBean),
                new DialogFinishNotPass.OnConfirmListener() {
                    @Override
                    public void onConfirm(String ids, String reason) {
                        mDialogLoader.showProgressDialog(true, "正在提交", "请稍候...");
                        mPresenter.rework(UserManager.getInstance().getToken(),
                                mRepairId, ids, reason
                        );
                    }
                }).measureSize().show();
    }
}